<?php
//商品分类控制器
class CategoryController extends BaseController {
	public function indexAction(){
		//获取所有分类
		$categoryModel = new CategoryModel('category');
		$cats = $categoryModel->getCats();
		//载入视图
		include CUR_VIEW_PATH . "cat_list.html";
	}

	//显示添加表单
	public function addAction(){
		//获取所有的商品分类
		$categoryModel = new CategoryModel('category');
		$cats = $categoryModel->getCats();
		//var_dump($cats);
		//载入模板页面
		include CUR_VIEW_PATH . "cat_add.html";
	}

	//商品分类入库
	public function insertAction(){
		//1.收集表单数据,以关联数组的形式来收集 CTRL + SHIFT + D
		$data['cat_name'] = trim($_POST['cat_name']);
		$data['parent_id'] = $_POST['parent_id'];
		$data['unit'] = trim($_POST['unit']);
		$data['sort_order'] = $_POST['sort_order'];
		$data['cat_desc'] = trim($_POST['cat_desc']);
		$data['is_show'] = $_POST['is_show'];
		//进行实体转义
		//引入辅助函数
		$this->helper('input');
		$data=deepspecialchars($data);
//		$data['cat_desc']=htmlspecialchars($data['cat_desc']);
		//2.对数据进行验证和处理
		if ($data['cat_name'] == ''){
			$this->jump('index.php?p=admin&c=category&a=add','分类名称不能为空');
		}
		//3.调用模型完成入库，并给出提示
		$categoryModel = new CategoryModel('category'); //注意传不带前缀表名
		if ($categoryModel->insert($data)) {
			//成功
			$this->jump('index.php?p=admin&c=category&a=index','添加成功',2);
		} else {
			//失败
			$this->jump('index.php?p=admin&c=category&a=add','添加失败');
		} 
	}
	public function editAction(){
		//获取分类id
		$cat_id = $_GET['cat_id'] + 0; //思考
		//获取该分类的信息
		$categoryModel = new CategoryModel('category');
		//此处还有了基础model中定义的selectByPk方法
		$cat = $categoryModel->selectByPk($cat_id);
		//获取所有的分类
		$cats = $categoryModel->getCats();
		//载入页面
		include CUR_VIEW_PATH . "cat_edit.html";
	}

	//分类更新
	public function updateAction(){
		//1.收集表单数据
		$data['cat_name'] = trim($_POST['cat_name']);
		$data['parent_id'] = $_POST['parent_id'];
		$data['unit'] = trim($_POST['unit']);
		$data['sort_order'] = $_POST['sort_order'];
		$data['cat_desc'] = trim($_POST['cat_desc']);
		$data['is_show'] = $_POST['is_show'];
		$data['cat_id'] = $_POST['cat_id']; //别忘了我
		//2.数据的处理和验证
		if ($data['cat_name'] == ''){
			$this->jump('index.php?p=admin&c=category&a=add','分类名称不能为空');
		}
		//不能将当前分类或者后代分类作为其上级分类
		$categoryModel = new CategoryModel('category'); //注意传不带前缀表名
		$ids = $categoryModel->getSubIds($data['cat_id']);
		if (in_array($data['parent_id'], $ids)) {
			//不允许
			$this->jump('index.php?p=admin&c=category&a=edit&cat_id='.$data['cat_id'],'不能将当前分类或者后代分类作为其上级分类');
		}
		//3.调用模型完成入库并给出提示
		if ($categoryModel->update($data)) {
			//成功
			$this->jump('index.php?p=admin&c=category&a=index','更新成功',2);
		} else {
			//失败
			$this->jump('index.php?p=admin&c=category&a=edit&cat_id='.$data['cat_id'],'更新失败');
		} 
	}

	//删除分类
	public function deleteAction(){
		//获取cat_id
		$cat_id = $_GET['cat_id'] + 0;
		//调用模型
		$categoryModel = new CategoryModel('category');
		//提示一下，当前分类有后代分类，请先删除后代分类
		$ids = $categoryModel->getSubIds($cat_id); //注意，至少有一个，就是当前的cat_id
		if (count($ids) > 1) {
			$this->jump('index.php?p=admin&c=category&a=index','当前分类不是叶子节点，请先删除后代分类');
		}
		if ($categoryModel->delete($cat_id)) {
			$this->jump('index.php?p=admin&c=category&a=index','删除成功',2);
		} else {
			$this->jump('index.php?p=admin&c=category&a=index','删除失败');
		}
	}
}